
public class List {


	List(){
		first = null;
		last = null;
		lengthList = 0;
	}
	
	public int getLengthList(){
		return lengthList;
	}
	public void LengthListAdd() {
		lengthList++;
	}
	public void LengthListSub() {
		lengthList--;
	}
	
	public void print() {
		Node current = first;
		for (int i = 0; i < lengthList; i++){
			System.out.print(current.getContent() + " ");
			System.out.print(";\t");
			current = current.next();
		} 
		System.out.println();
	}
	
	public void insert(Node x, int p) {
		/*System.out.print("P: " + p + ": "); Testing!
		for (int e : x.getContent())
			System.out.print(e);
		System.out.print(" " + lengthList );
		System.out.println(); */
		if (p > (lengthList) )
			throw new IllegalArgumentException("p is to hight for our list.");
		//System.out.println(lengthList);
		if (p == 0) {
			Node help = first;
			x.setPrevious(null);
			x.setNext(help);
			if (lengthList != 0 ) 
				help.setPrevious(x);
			else
				last = x;
			first = x;
			
		}
		if (p == lengthList && p != 0) {
			Node help = last;
			x.setPrevious(help);
			x.setNext(null);
			help.setNext(x);
			last = x;
		}
		if (p > 0 && p < lengthList) {
			Node help = first;
			/*System.out.println("Help = first (p>0 && p < lengthList-1)"); Testing!
			for (int e : help.getContent())
				System.out.print(e);
			System.out.println(); */
			for (int i=1; i<=p; i++) {
				if (help.next() == null)
					throw new NullPointerException();
				else
					help = help.next();
				/*System.out.print(i + ":"); Testing!
				for (int e : help.getContent())
					System.out.print(e);
				System.out.println();*/
			}
			x.setNext(help);
			/*System.out.print("x.next.content: ");Testing!
			for (int f : x.next().getContent())
				System.out.print(f);
			System.out.println();*/
			x.setPrevious(help.previous());
			/*System.out.print("help.previous.content: ");
			for (int e : help.previous().getContent()) Testing!
				System.out.print(e);
			System.out.println();
			System.out.print("x.previous.content: ");
			for (int e : x.previous().getContent())
				System.out.print(e);
			System.out.println();*/
			help.previous().setNext(x);
			/*System.out.print("help.previus.next.content: "); Testing!
			for (int e : help.previous().next().getContent())
				System.out.print(e);
			System.out.println();*/
			help.setPrevious(x);
			/*System.out.print("help.previous.contetn: "); Testing!
			for (int e : help.previous().getContent())
				System.out.print(e);
			System.out.println(); */
		}		
		lengthList++;
	}
	
	public Node remove (int p) {
		if (p < 0 || p >= lengthList) 
			throw new IllegalArgumentException();
		Node help = first;
		for (int i=1; i <=p; i++) {
			help = help.next();
			/*System.out.print(i + " help.contetn: "); Testing!
			for (int e : help.getContent())
				System.out.print(e);
			System.out.println(); */
		}
		if (help.previous() == null){
			first = help.next();
			help.setPrevious(null);
		}
		else
			help.previous().setNext(help.next());
		if (help.next() == null){
			last = help.previous();
			help.setNext(null);
		}
		else {
			help.next().setPrevious(help.previous());
		}
		lengthList--;
		return help;
	}
	
	public int search(Node x) {
		Node help = first;
		for (int i=0; i < lengthList; i++){
			if (x.getContent() == help.getContent())
				return i;
			help = help.next();
		}
		return -1;
	}
	
	//data elements	
	private int lengthList;
	Node first;
	Node last;	
	
}
